package org.jget.view;

import javax.swing.JDialog;
import java.awt.Dimension;

import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.NumberFormat;

import javax.swing.JCheckBox;
import javax.swing.JButton;

public class ConfigurationDialog extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3283773327896815096L;
	private JPanel jContentPane = null;
	private JLabel jLabel = null;
	private JFormattedTextField maxDownloadsJTextField = null;
	private JLabel jLabel1 = null;
	private JFormattedTextField downRateJTextField = null;
	private JLabel jLabel2 = null;
	private JFormattedTextField upRateJTextField = null;
	private JLabel jLabel3 = null;
	private JFormattedTextField portRangeStartJTextField = null;
	private JLabel jLabel4 = null;
	private JFormattedTextField portRangeEndJTextField = null;
	private JLabel jLabel5 = null;
	private JCheckBox natPortForwardingJCheckBox = null;
	private JButton okJButton = null;
	protected boolean ok = false;

	/**
	 * This method initializes
	 * 
	 */
	public ConfigurationDialog(JFrame fr) {
		super(fr, "Configuration", false);
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 */
	private void initialize() {
		this.setSize(new Dimension(379, 190));
		this.setContentPane(getJContentPane());
		this.setTitle("Configuration");
		this.setModal(true);
		// Handle window closing correctly.
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		// setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)
		addWindowListener(new WA());
	}

	public void setDownRate(int downRate) {
		this.downRateJTextField.setText(Integer.toString(downRate));
	}

	public void setMaxDownloads(int maxDownloads) {
		this.maxDownloadsJTextField.setText(Integer.toString(maxDownloads));
	}

	public void setNatPortForwarding(boolean natPortForwarding) {
		this.natPortForwardingJCheckBox.setSelected(natPortForwarding);
	}

	public void setPortRangeEnd(int portRangeEnd) {
		this.portRangeEndJTextField.setText(Integer.toString(portRangeEnd));
	}

	public void setPortRangeStart(int portRangeStart) {
		this.portRangeStartJTextField.setText(Integer.toString(portRangeStart));
	}

	public void setUpRate(int upRate) {
		this.upRateJTextField.setText(Integer.toString(upRate));
	}

	public int getDownRate() {
		return Integer.parseInt(this.downRateJTextField.getText());
	}

	public int getMaxDownloads() {
		return Integer.parseInt(this.maxDownloadsJTextField.getText());
	}

	public boolean isNatPortForwarding() {
		return this.natPortForwardingJCheckBox.isSelected();
	}

	public int getPortRangeEnd() {
		return Integer.parseInt(this.portRangeEndJTextField.getText());
	}

	public int getPortRangeStart() {
		return Integer.parseInt(this.portRangeStartJTextField.getText());
	}

	public int getUpRate() {
		return Integer.parseInt(this.upRateJTextField.getText());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
			gridBagConstraints10.gridx = 2;
			gridBagConstraints10.gridy = 6;
			GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
			gridBagConstraints9.gridx = 2;
			gridBagConstraints9.gridy = 5;
			GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
			gridBagConstraints8.gridx = 0;
			gridBagConstraints8.gridy = 5;
			jLabel5 = new JLabel();
			jLabel5.setText("UPnP NAT port forwarding");
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.fill = GridBagConstraints.BOTH;
			gridBagConstraints7.gridy = 4;
			gridBagConstraints7.weightx = 1.0;
			gridBagConstraints7.gridx = 2;
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.gridx = 0;
			gridBagConstraints6.gridy = 4;
			jLabel4 = new JLabel();
			jLabel4.setText("BitTorrent port range end");
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.fill = GridBagConstraints.BOTH;
			gridBagConstraints5.gridy = 3;
			gridBagConstraints5.weightx = 1.0;
			gridBagConstraints5.gridx = 2;
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.gridx = 0;
			gridBagConstraints4.gridy = 3;
			jLabel3 = new JLabel();
			jLabel3.setText("BitTorrent port range start");
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.fill = GridBagConstraints.BOTH;
			gridBagConstraints3.gridy = 2;
			gridBagConstraints3.weightx = 1.0;
			gridBagConstraints3.gridx = 2;
			GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
			gridBagConstraints21.gridx = 0;
			gridBagConstraints21.gridy = 2;
			jLabel2 = new JLabel();
			jLabel2.setText("Max. upload rate (KB/s) [0 means unlimited]");
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.fill = GridBagConstraints.BOTH;
			gridBagConstraints11.gridy = 1;
			gridBagConstraints11.weightx = 1.0;
			gridBagConstraints11.gridx = 2;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 0;
			gridBagConstraints2.gridy = 1;
			jLabel1 = new JLabel();
			jLabel1.setText("Max. download rate (KB/s) [0 means unlimited]");
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.fill = GridBagConstraints.BOTH;
			gridBagConstraints1.gridy = 0;
			gridBagConstraints1.weightx = 1.0;
			gridBagConstraints1.gridwidth = 2;
			gridBagConstraints1.gridx = 1;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.gridy = 0;
			jLabel = new JLabel();
			jLabel.setText("Maximum number of concurrent downloads");
			jContentPane = new JPanel();
			jContentPane.setLayout(new GridBagLayout());
			jContentPane.add(jLabel, gridBagConstraints);
			jContentPane.add(getMaxDownloadsJTextField(), gridBagConstraints1);
			jContentPane.add(jLabel1, gridBagConstraints2);
			jContentPane.add(getDownRateJTextField(), gridBagConstraints11);
			jContentPane.add(jLabel2, gridBagConstraints21);
			jContentPane.add(getUpRateJTextField(), gridBagConstraints3);
			jContentPane.add(jLabel3, gridBagConstraints4);
			jContentPane
					.add(getPortRangeStartJTextField(), gridBagConstraints5);
			jContentPane.add(jLabel4, gridBagConstraints6);
			jContentPane.add(getPortRangeEndJTextField(), gridBagConstraints7);
			jContentPane.add(jLabel5, gridBagConstraints8);
			jContentPane.add(getNatPortForwardingJCheckBox(),
					gridBagConstraints9);
			jContentPane.add(getOkJButton(), gridBagConstraints10);
		}
		return jContentPane;
	}

	/**
	 * This method initializes maxDownloadsJTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getMaxDownloadsJTextField() {
		if (maxDownloadsJTextField == null) {
			maxDownloadsJTextField = new JFormattedTextField(
					NumberFormat.getInstance());
			maxDownloadsJTextField
					.setFocusLostBehavior(JFormattedTextField.COMMIT_OR_REVERT);
		}
		return maxDownloadsJTextField;
	}

	/**
	 * This method initializes downRateJTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getDownRateJTextField() {
		if (downRateJTextField == null) {
			downRateJTextField = new JFormattedTextField(
					NumberFormat.getInstance());
			downRateJTextField
					.setFocusLostBehavior(JFormattedTextField.COMMIT_OR_REVERT);
		}
		return downRateJTextField;
	}

	/**
	 * This method initializes upRateJTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getUpRateJTextField() {
		if (upRateJTextField == null) {
			upRateJTextField = new JFormattedTextField(
					NumberFormat.getInstance());
			upRateJTextField
					.setFocusLostBehavior(JFormattedTextField.COMMIT_OR_REVERT);
		}
		return upRateJTextField;
	}

	/**
	 * This method initializes portRangeStartJTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getPortRangeStartJTextField() {
		if (portRangeStartJTextField == null) {
			portRangeStartJTextField = new JFormattedTextField(
					NumberFormat.getInstance());
			portRangeStartJTextField
					.setFocusLostBehavior(JFormattedTextField.COMMIT_OR_REVERT);
		}
		return portRangeStartJTextField;
	}

	/**
	 * This method initializes portRangeEndJTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getPortRangeEndJTextField() {
		if (portRangeEndJTextField == null) {
			portRangeEndJTextField = new JFormattedTextField(
					NumberFormat.getInstance());
			portRangeEndJTextField
					.setFocusLostBehavior(JFormattedTextField.COMMIT_OR_REVERT);
		}
		return portRangeEndJTextField;
	}

	/**
	 * This method initializes natPortForwardingJCheckBox
	 * 
	 * @return javax.swing.JCheckBox
	 */
	private JCheckBox getNatPortForwardingJCheckBox() {
		if (natPortForwardingJCheckBox == null) {
			natPortForwardingJCheckBox = new JCheckBox();
		}
		return natPortForwardingJCheckBox;
	}

	/**
	 * This method initializes okJButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getOkJButton() {
		if (okJButton == null) {
			okJButton = new JButton();
			okJButton.setText("Ok");
			okJButton.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					ConfigurationDialog.this.ok = true;
					ConfigurationDialog.this.setVisible(false);
				}
			});
		}
		return okJButton;
	}

	class WA extends WindowAdapter {
		public void windowClosing(WindowEvent ev) {
			setVisible(false);
		}
	}

	public boolean isOk() {
		return ok;
	}

} // @jve:decl-index=0:visual-constraint="10,10"
